/*
 * Copyright (c) Hunan Goke,Chengdu Goke,Shandong Goke. 2021. All rights reserved.
 */
#ifndef __COMMON_CIPHER__
#define __COMMON_CIPHER__

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif

#define  ERR_CODE_CIPHER_NOT_INIT                     (GK_S32)(0x804D0001)
#define  ERR_CODE_CIPHER_INVALID_HANDLE               (GK_S32)(0x804D0002)
#define  ERR_CODE_CIPHER_INVALID_POINT                (GK_S32)(0x804D0003)
#define  ERR_CODE_CIPHER_INVALID_PARA                 (GK_S32)(0x804D0004)
#define  ERR_CODE_CIPHER_FAILED_INIT                  (GK_S32)(0x804D0005)
#define  ERR_CODE_CIPHER_FAILED_GETHANDLE             (GK_S32)(0x804D0006)
#define  ERR_CODE_CIPHER_FAILED_RELEASEHANDLE         (GK_S32)(0x804D0007)
#define  ERR_CODE_CIPHER_FAILED_CONFIGAES             (GK_S32)(0x804D0008)
#define  ERR_CODE_CIPHER_FAILED_CONFIGDES             (GK_S32)(0x804D0009)
#define  ERR_CODE_CIPHER_FAILED_ENCRYPT               (GK_S32)(0x804D000A)
#define  ERR_CODE_CIPHER_FAILED_DECRYPT               (GK_S32)(0x804D000B)
#define  ERR_CODE_CIPHER_BUSY                         (GK_S32)(0x804D000C)
#define  ERR_CODE_CIPHER_NO_AVAILABLE_RNG             (GK_S32)(0x804D000D)
#define  ERR_CODE_CIPHER_FAILED_MEM                   (GK_S32)(0x804D000E)
#define  ERR_CODE_CIPHER_UNAVAILABLE                  (GK_S32)(0x804D000F)
#define  ERR_CODE_CIPHER_OVERFLOW                     (GK_S32)(0x804D0010)
#define  ERR_CODE_CIPHER_HARD_STATUS                  (GK_S32)(0x804D0011)
#define  ERR_CODE_CIPHER_TIMEOUT                      (GK_S32)(0x804D0012)
#define  ERR_CODE_CIPHER_UNSUPPORTED                  (GK_S32)(0x804D0013)
#define  ERR_CODE_CIPHER_REGISTER_IRQ                 (GK_S32)(0x804D0014)
#define  ERR_CODE_CIPHER_ILLEGAL_UUID                 (GK_S32)(0x804D0015)
#define  ERR_CODE_CIPHER_ILLEGAL_KEY                  (GK_S32)(0x804D0016)
#define  ERR_CODE_CIPHER_INVALID_ADDR                 (GK_S32)(0x804D0017)
#define  ERR_CODE_CIPHER_INVALID_LENGTH               (GK_S32)(0x804D0018)
#define  ERR_CODE_CIPHER_ILLEGAL_DATA                 (GK_S32)(0x804D0019)
#define  ERR_CODE_CIPHER_RSA_SIGN                     (GK_S32)(0x804D001A)
#define  ERR_CODE_CIPHER_RSA_VERIFY                   (GK_S32)(0x804D001B)
#define  ERR_CODE_CIPHER_MEMSET_S_FAILED              (GK_S32)(0x804D001C)
#define  ERR_CODE_CIPHER_MEMCPY_S_FAILED              (GK_S32)(0x804D001D)
#define  ERR_CODE_CIPHER_RSA_CRYPT_FAILED             (GK_S32)(0x804D001E)

#define SM2_LEN_IN_WROD                   (8)

#define SM2_LEN_IN_BYTE                   (SM2_LEN_IN_WROD * 4)

#define CIPHER_IV_CHANGE_ONE_PKG        (1)

#define CIPHER_IV_CHANGE_ALL_PKG        (2)

typedef enum UNF_CIPHER_WORK_MODE_E
{
    UNF_CIPHER_WORK_MODE_ECB,
    UNF_CIPHER_WORK_MODE_CBC,
    UNF_CIPHER_WORK_MODE_CFB,
    UNF_CIPHER_WORK_MODE_OFB,
    UNF_CIPHER_WORK_MODE_CTR,
    UNF_CIPHER_WORK_MODE_CCM,
    UNF_CIPHER_WORK_MODE_GCM,
    UNF_CIPHER_WORK_MODE_CBC_CTS,
    UNF_CIPHER_WORK_MODE_BUTT,
    UNF_CIPHER_WORK_MODE_INVALID  = 0xffffffff,
}UNF_CIPHER_WORK_MODE_E;

typedef enum UNF_CIPHER_ALG_E
{
    UNF_CIPHER_ALG_DES           = 0x0,
    UNF_CIPHER_ALG_3DES          = 0x1,
    UNF_CIPHER_ALG_AES           = 0x2,
    UNF_CIPHER_ALG_SM1           = 0x3,
    UNF_CIPHER_ALG_SM4           = 0x4,
    UNF_CIPHER_ALG_DMA           = 0x5,
    UNF_CIPHER_ALG_BUTT          = 0x6,
    UNF_CIPHER_ALG_INVALID       = 0xffffffff,
}UNF_CIPHER_ALG_E;

typedef enum UNF_CIPHER_KEY_LENGTH_E
{
    UNF_CIPHER_KEY_AES_128BIT    = 0x0,
    UNF_CIPHER_KEY_AES_192BIT    = 0x1,
    UNF_CIPHER_KEY_AES_256BIT    = 0x2,
    UNF_CIPHER_KEY_DES_3KEY      = 0x2,
    UNF_CIPHER_KEY_DES_2KEY      = 0x3,
    UNF_CIPHER_KEY_DEFAULT       = 0x0,
    UNF_CIPHER_KEY_INVALID       = 0xffffffff,
}UNF_CIPHER_KEY_LENGTH_E;

typedef enum UNF_CIPHER_BIT_WIDTH_E
{
    UNF_CIPHER_BIT_WIDTH_64BIT   = 0x0,
    UNF_CIPHER_BIT_WIDTH_8BIT    = 0x1,
    UNF_CIPHER_BIT_WIDTH_1BIT    = 0x2,
    UNF_CIPHER_BIT_WIDTH_128BIT  = 0x3,
    UNF_CIPHER_BIT_WIDTH_INVALID = 0xffffffff,
}UNF_CIPHER_BIT_WIDTH_E;

typedef struct TEE_CIPHER_CTRL_CHANGE_FLAG_S
{
    GK_U32   bit1IV:2;
    GK_U32   bitsResv:30;
}UNF_CIPHER_CTRL_CHANGE_FLAG_S;

typedef enum TEE_CIPHER_CA_TYPE_E
{
    UNF_CIPHER_KEY_SRC_USER       = 0x0,
    UNF_CIPHER_KEY_SRC_KLAD_1,
    UNF_CIPHER_KEY_SRC_KLAD_2,
    UNF_CIPHER_KEY_SRC_KLAD_3,
    UNF_CIPHER_KEY_SRC_BUTT,
    UNF_CIPHER_KEY_SRC_INVALID = 0xffffffff,
}UNF_CIPHER_CA_TYPE_E;

typedef enum
{
    UNF_CIPHER_KLAD_TARGET_AES       = 0x0,
    UNF_CIPHER_KLAD_TARGET_RSA,
    UNF_CIPHER_KLAD_TARGET_BUTT,
}UNF_CIPHER_KLAD_TARGET_E;

typedef enum
{
    UNF_CIPHER_TYPE_NORMAL  = 0x0,
    UNF_CIPHER_TYPE_COPY_AVOID,
    UNF_CIPHER_TYPE_BUTT,
    UNF_CIPHER_TYPE_INVALID = 0xffffffff,
}UNF_CIPHER_TYPE_E;

typedef struct
{
    UNF_CIPHER_TYPE_E enCipherType;
}UNF_CIPHER_ATTS_S;

typedef enum UNF_CIPHER_SM1_ROUND_E
{
    UNF_CIPHER_SM1_ROUND_08 = 0x00,
    UNF_CIPHER_SM1_ROUND_10 = 0x01,
    UNF_CIPHER_SM1_ROUND_12 = 0x02,
    UNF_CIPHER_SM1_ROUND_14 = 0x03,
    UNF_CIPHER_SM1_ROUND_BUTT,
    UNF_CIPHER_SM1_ROUND_INVALID  = 0xffffffff,
}UNF_CIPHER_SM1_ROUND_E;

typedef struct UNF_CIPHER_CTRL_S
{
    GK_U32 u32Key[8];
    GK_U32 u32IV[4];
    GK_BOOL bKeyByCA;
    UNF_CIPHER_CA_TYPE_E enCaType;
    UNF_CIPHER_ALG_E enAlg;
    UNF_CIPHER_BIT_WIDTH_E enBitWidth;
    UNF_CIPHER_WORK_MODE_E enWorkMode;
    UNF_CIPHER_KEY_LENGTH_E enKeyLen;
    UNF_CIPHER_CTRL_CHANGE_FLAG_S stChangeFlags;
} UNF_CIPHER_CTRL_S;

typedef struct UNF_CIPHER_CTRL_AES_S
{
    GK_U32 u32EvenKey[8];
    GK_U32 u32OddKey[8];
    GK_U32 u32IV[4];
    UNF_CIPHER_BIT_WIDTH_E enBitWidth;
    UNF_CIPHER_KEY_LENGTH_E enKeyLen;
    UNF_CIPHER_CTRL_CHANGE_FLAG_S stChangeFlags;
} UNF_CIPHER_CTRL_AES_S;

typedef struct UNF_CIPHER_CTRL_AES_CCM_GCM_S
{
    GK_U32 u32Key[8];
    GK_U32 u32IV[4];
    UNF_CIPHER_KEY_LENGTH_E enKeyLen;
    GK_U32 u32IVLen;
    GK_U32 u32TagLen;
    GK_U32 u32ALen;
    GK_SIZE_T szAPhyAddr;
} UNF_CIPHER_CTRL_AES_CCM_GCM_S;

typedef struct UNF_CIPHER_CTRL_DES_S
{
    GK_U32 u32Key[2];
    GK_U32 u32IV[2];
    UNF_CIPHER_BIT_WIDTH_E enBitWidth;
    UNF_CIPHER_CTRL_CHANGE_FLAG_S stChangeFlags;
} UNF_CIPHER_CTRL_DES_S;

typedef struct UNF_CIPHER_CTRL_3DES_S
{
    GK_U32 u32Key[6];
    GK_U32 u32IV[2];
    UNF_CIPHER_BIT_WIDTH_E enBitWidth;
    UNF_CIPHER_KEY_LENGTH_E enKeyLen;
    UNF_CIPHER_CTRL_CHANGE_FLAG_S stChangeFlags;
} UNF_CIPHER_CTRL_3DES_S;

typedef struct UNF_CIPHER_CTRL_SM1_S
{
    GK_U32 u32EK[4];
    GK_U32 u32AK[4];
    GK_U32 u32SK[4];
    GK_U32 u32IV[4];
    UNF_CIPHER_BIT_WIDTH_E enBitWidth;
    UNF_CIPHER_SM1_ROUND_E enSm1Round;
    UNF_CIPHER_CTRL_CHANGE_FLAG_S stChangeFlags;
} UNF_CIPHER_CTRL_SM1_S;

typedef struct UNF_CIPHER_CTRL_SM4_S
{
    GK_U32 u32Key[4];
    GK_U32 u32IV[4];
    UNF_CIPHER_CTRL_CHANGE_FLAG_S stChangeFlags;
} UNF_CIPHER_CTRL_SM4_S;

typedef struct UNF_CIPHER_CTRL_EX_S
{
    UNF_CIPHER_ALG_E enAlg;
    UNF_CIPHER_WORK_MODE_E enWorkMode;
    GK_BOOL bKeyByCA;
    GK_VOID *pParam;
} UNF_CIPHER_CTRL_EX_S;

typedef struct UNF_CIPHER_DATA_S
{
    GK_SIZE_T szSrcPhyAddr;
    GK_SIZE_T szDestPhyAddr;
    GK_U32 u32ByteLength;
    GK_BOOL bOddKey;
} UNF_CIPHER_DATA_S;

typedef enum UNF_CIPHER_HASH_TYPE_E
{
    UNF_CIPHER_HASH_TYPE_SHA1,
    UNF_CIPHER_HASH_TYPE_SHA224,
    UNF_CIPHER_HASH_TYPE_SHA256,
    UNF_CIPHER_HASH_TYPE_SHA384,
    UNF_CIPHER_HASH_TYPE_SHA512,
    UNF_CIPHER_HASH_TYPE_HMAC_SHA1,
    UNF_CIPHER_HASH_TYPE_HMAC_SHA224,
    UNF_CIPHER_HASH_TYPE_HMAC_SHA256,
    UNF_CIPHER_HASH_TYPE_HMAC_SHA384,
    UNF_CIPHER_HASH_TYPE_HMAC_SHA512,
    UNF_CIPHER_HASH_TYPE_SM3,
    UNF_CIPHER_HASH_TYPE_BUTT,
    UNF_CIPHER_HASH_TYPE_INVALID  = 0xffffffff,
}UNF_CIPHER_HASH_TYPE_E;

typedef struct
{
    GK_U8 *pu8HMACKey;
    GK_U32 u32HMACKeyLen;
    UNF_CIPHER_HASH_TYPE_E eShaType;
}UNF_CIPHER_HASH_ATTS_S;

typedef enum UNF_CIPHER_RSA_ENC_SCHEME_E
{
    UNF_CIPHER_RSA_ENC_SCHEME_NO_PADDING,
    UNF_CIPHER_RSA_ENC_SCHEME_BLOCK_TYPE_0,
    UNF_CIPHER_RSA_ENC_SCHEME_BLOCK_TYPE_1,
    UNF_CIPHER_RSA_ENC_SCHEME_BLOCK_TYPE_2,
    UNF_CIPHER_RSA_ENC_SCHEME_RSAES_OAEP_SHA1,
    UNF_CIPHER_RSA_ENC_SCHEME_RSAES_OAEP_SHA224,
    UNF_CIPHER_RSA_ENC_SCHEME_RSAES_OAEP_SHA256,
    UNF_CIPHER_RSA_ENC_SCHEME_RSAES_OAEP_SHA384,
    UNF_CIPHER_RSA_ENC_SCHEME_RSAES_OAEP_SHA512,
    UNF_CIPHER_RSA_ENC_SCHEME_RSAES_PKCS1_V1_5,
    UNF_CIPHER_RSA_ENC_SCHEME_BUTT,
    UNF_CIPHER_RSA_ENC_SCHEME_INVALID  = 0xffffffff,
}UNF_CIPHER_RSA_ENC_SCHEME_E;

typedef enum UNF_CIPHER_RSA_SIGN_SCHEME_E
{
    UNF_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_V15_SHA1 = 0x100,
    UNF_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_V15_SHA224,
    UNF_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_V15_SHA256,
    UNF_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_V15_SHA384,
    UNF_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_V15_SHA512,
    UNF_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_PSS_SHA1,
    UNF_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_PSS_SHA224,
    UNF_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_PSS_SHA256,
    UNF_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_PSS_SHA384,
    UNF_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_PSS_SHA512,
    UNF_CIPHER_RSA_SIGN_SCHEME_BUTT,
    UNF_CIPHER_RSA_SIGN_SCHEME_INVALID  = 0xffffffff,
}UNF_CIPHER_RSA_SIGN_SCHEME_E;

typedef struct
{
    GK_U8  *pu8N;
    GK_U8  *pu8E;
    GK_U16 u16NLen;
    GK_U16 u16ELen;
}UNF_CIPHER_RSA_PUB_KEY_S;

typedef struct
{
    GK_U8 *pu8N;
    GK_U8 *pu8E;
    GK_U8 *pu8D;
    GK_U8 *pu8P;
    GK_U8 *pu8Q;
    GK_U8 *pu8DP;
    GK_U8 *pu8DQ;
    GK_U8 *pu8QP;
    GK_U16 u16NLen;
    GK_U16 u16ELen;
    GK_U16 u16DLen;
    GK_U16 u16PLen;
    GK_U16 u16QLen;
    GK_U16 u16DPLen;
    GK_U16 u16DQLen;
    GK_U16 u16QPLen;
}UNF_CIPHER_RSA_PRI_KEY_S;

typedef struct
{
    UNF_CIPHER_RSA_ENC_SCHEME_E enScheme;
    UNF_CIPHER_RSA_PUB_KEY_S stPubKey;
    UNF_CIPHER_CA_TYPE_E enCaType;
}UNF_CIPHER_RSA_PUB_ENC_S;

typedef struct
{
    UNF_CIPHER_RSA_ENC_SCHEME_E enScheme;
    UNF_CIPHER_RSA_PRI_KEY_S stPriKey;
    UNF_CIPHER_CA_TYPE_E enCaType;
}UNF_CIPHER_RSA_PRI_ENC_S;

typedef struct
{
    UNF_CIPHER_RSA_SIGN_SCHEME_E enScheme;
    UNF_CIPHER_RSA_PRI_KEY_S stPriKey;
    UNF_CIPHER_CA_TYPE_E enCaType;
}UNF_CIPHER_RSA_SIGN_S;

typedef struct
{
    UNF_CIPHER_RSA_SIGN_SCHEME_E enScheme;
    UNF_CIPHER_RSA_PUB_KEY_S stPubKey;
}UNF_CIPHER_RSA_VERIFY_S;

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif

#endif

